package com.rfsp.common.init;

import com.rfsp.common.cache.AreaCache;
import com.rfsp.common.cache.DictCache;
import com.rfsp.common.pojo.vo.AreaVO;
import com.rfsp.common.service.AreaService;
import com.rfsp.common.shiro.ShiroUtils;
import com.rfsp.common.util.JsonUtil;
import com.rfsp.common.util.TreeUtil;
import com.rfsp.common.enums.DictEnum;
import com.rfsp.common.service.PriviService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import java.util.stream.Collectors;

@Component
public class InitDict implements ApplicationRunner {
	
	private static final Logger logger = LoggerFactory.getLogger(InitDict.class);

    @Autowired
    ServletContext application;

    @Autowired
	PriviService priviService;
	
	@Autowired
	AreaService areaService;
	
	@Autowired
    AreaCache areaCache;

	@Autowired
	DictCache dictCache;

    @Override
    public void run(ApplicationArguments args) throws Exception {

    	try {
			logger.info("init PrivTree cache begin!");
			priviService.initRightByRoleCode();
			logger.info("init PrivTree cache finish!");

            areaCache.areaList = areaService.queryAllAreas();
            areaCache.areaList.forEach(a-> areaCache.areaMap.put(a.getId(), a));
            areaCache.provinceList = areaCache.areaList.stream().filter(a->a.getParent().equals("000000")).collect(Collectors.toList());
            areaCache.cityList = areaCache.areaList.stream().filter(a->a.getParent().endsWith("0000")).collect(Collectors.toList());
            areaCache.countyList = areaCache.areaList.stream().filter(a->a.getChildren()==null).collect(Collectors.toList());

            areaCache.treeList = TreeUtil.makeTree(areaCache.areaList, a->a.getParent().equals("000000"), (a, b)->a.getId().equals(b.getParent()), AreaVO::setChildren);
            areaCache.treeList.forEach(a->{
                areaCache.treeMap.put(a.getId(), a.getChildren());
                a.getChildren().forEach(b-> areaCache.treeMap.put(b.getId(), b.getChildren()));
            });

            logger.info("areaCache.treeList="+areaCache.treeList);
			areaCache.areaJson = JsonUtil.toJson(areaCache.treeList);
	    	ShiroUtils.setAreaCodes(areaCache.areaJson);

            logger.info("areaCache.areaMap={}", areaCache.areaMap);

	        application.setAttribute(DictEnum.area.name(), areaCache);
	        application.setAttribute(DictEnum.areaCache.name(), areaCache);
            application.setAttribute(DictEnum.areaList.name(), areaCache.areaList);
	        application.setAttribute(DictEnum.treeList.name(), areaCache.treeList);
	        application.setAttribute(DictEnum.areaJson.name(), areaCache.areaJson);
	        application.setAttribute(DictEnum.areaMap.name(), areaCache.areaMap);
//	        application.setAttribute(DictEnum.areaTreeMap.name(), areaCache.areaTreeMap);
	        application.setAttribute(DictEnum.treeMap.name(), areaCache.treeMap);
	        application.setAttribute(DictEnum.provinceList.name(), areaCache.provinceList);
	        application.setAttribute(DictEnum.cityList.name(), areaCache.cityList);
	        application.setAttribute(DictEnum.countyList.name(), areaCache.countyList);



			application.setAttribute(DictEnum.dict.name(), dictCache);
			application.setAttribute(DictEnum.DictMap.name(), dictCache.DictMap);
			application.setAttribute(DictEnum.DictList.name(), dictCache.DictList);
	        
		} catch (Exception e) {
			logger.error("init PrivTree cache error!",e);
		}
        
    }
    
}
